package com.parkingwang.vehiclekeyboard.demo.keyboard

import android.content.DialogInterface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v4.app.FragmentManager
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import com.parkingwang.keyboard.KeyboardInputController
import com.parkingwang.keyboard.OnInputChangedListener
import com.parkingwang.keyboard.view.InputView
import com.parkingwang.keyboard.view.KeyboardView
import com.parkingwang.vehiclekeyboard.demo.R

/**
 * 车牌软键盘弹窗
 * @author 李晓强 (lixiaoqiang@parkingwang.com)
 * @since 2019/3/1
 */
class VehicleBottomDialog : DialogFragment() {

    private var callback: KeyboardCallback? = null
    private lateinit var mVplKeyboard: KeyboardView
    private lateinit var mInputView: InputView
    private lateinit var mBtnNewEnergy: Button
    private lateinit var btn_back: Button
    private var number = ""
    private lateinit var mController: KeyboardInputController
    private var isHide = true

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mController.updateNumber(number)
        mInputView.performLastPendingFieldView()
    }

    override fun onStart() {
        super.onStart()
        val win = dialog.window
        // 一定要设置Background，如果不设置，window属性设置无效
        win!!.setBackgroundDrawable(ColorDrawable(0x00000000))

        val dm = DisplayMetrics()
        activity!!.windowManager.defaultDisplay.getMetrics(dm)

        val params = win.attributes
        params.gravity = Gravity.BOTTOM
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
        win.attributes = params

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {

        var view: View = inflater.inflate(R.layout.dialog_vehicle1, container, false)

        mBtnNewEnergy = view.findViewById(R.id.btn_change_new_energy)
        btn_back = view.findViewById(R.id.btn_back)
        mVplKeyboard = view.findViewById(R.id.vpl_keyboard)
        mInputView = view.findViewById(R.id.input_view)
        mVplKeyboard.setCNTextSize(14F)
        mVplKeyboard.setENTextSize(14F)

        mController = KeyboardInputController.with(mVplKeyboard, mInputView)
        mController.useDefaultMessageHandler()
        mController.setSupportSpecial(true)
        mInputView.performFirstFieldView()
        mInputView.setSpecialMaxLength(20)
        // 隐藏确定按钮
        mVplKeyboard.keyboardEngine.setHideOKKey(isHide)

        mController
                .setDebugEnabled(true)
                .bindLockTypeProxy(object : KeyboardInputController.ButtonProxyImpl(mBtnNewEnergy) {
                    override fun onNumberTypeChanged(isNewEnergyType: Boolean) {
                        super.onNumberTypeChanged(isNewEnergyType)
                        if (isNewEnergyType) {
                            mBtnNewEnergy.setText("普通车")
                        } else {
                            mBtnNewEnergy.setText("新能源")
                        }
                    }
                })
        btn_back.setOnClickListener {
            mController.hideSpecial()

        }
        mController.addOnInputChangedListener(object : OnInputChangedListener {
            override fun onChanged(number: String, isCompleted: Boolean) {
                callback?.onKeyboardChanged(isCompleted, number)
                /*if (isCompleted) {
                    mPopupKeyboard.dismiss(dialog.window!!)
                }*/
            }

            override fun onCompleted(number: String, isAutoCompleted: Boolean) {
                callback?.onKeyboardCompleted(number)
                dismiss()
            }

            override fun onSpecialChange(isShow: Boolean) {
                mBtnNewEnergy.visibility = if (isShow) {
                    View.GONE
                } else {
                    View.VISIBLE
                }
                btn_back.visibility = if (isShow) {
                    View.VISIBLE
                } else {
                    View.GONE
                }
            }
        })
        return view
    }

    fun show(manager: FragmentManager) {
        super.show(manager, tag)
    }

    fun setKeyboardCallback(keyboardCallback: KeyboardCallback) {
        this.callback = keyboardCallback
    }

    fun updateNumber(number: String) {
        this.number = number
    }

    fun setHideOKKey(isHide: Boolean) {
        this.isHide = isHide
    }

    override fun onDismiss(dialog: DialogInterface?) {
        callback?.onKeyboardHiddenChanged(true)
        super.onDismiss(dialog)
    }
}